﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DAL;
namespace BAL
{
    public class BAL_MAYTINH
    {
        private static QuanLyTiemNetDataContext db = new QuanLyTiemNetDataContext();
        //thêm
        public static bool ThemMT(ref string err,string MaMay,string MaPhong)
        {
            db = new QuanLyTiemNetDataContext();
            bool them = false;
            try
            {
                MAYTINH mt = new MAYTINH();
                mt.MaMay = MaMay;
                mt.MaPhong = MaPhong;
                mt.DaCoNguoiThue = false;
                mt.DaXoa = false;
                db.MAYTINHs.InsertOnSubmit(mt);
                db.SubmitChanges();
                them = true;
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return them;
        }
        //cập nhật
        public static bool CapNhatMT(ref string err,string MaMay,string MaPhong)
        {
            db = new QuanLyTiemNetDataContext();
            bool update = false;
            try
            {
                MAYTINH tim=db.MAYTINHs.Single(mt=>mt.MaMay.Equals(MaMay));
                if(tim!=null)
                {
                    tim.MaPhong = MaPhong;
                    
                    db.SubmitChanges();
                    update = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return update;
        }
        //cập nhật đã có người thuê
        public static bool CapNhatDaCoNguoiThue(ref string err,string MaMay,bool DaCoNguoiThue)
        {
            db = new QuanLyTiemNetDataContext();
            bool update = false;
            try
            {
                MAYTINH tim = db.MAYTINHs.Single(mt => mt.MaMay.Equals(MaMay));
                if (tim != null)
                {
                    tim.DaCoNguoiThue = DaCoNguoiThue;

                    db.SubmitChanges();
                    update = true;
                }
            }
            catch (Exception e)
            {
                err = e.Message;
            }
            return update;
        }
        //xóa
        public static bool XoaMT(ref string err,string MaMay)
        {
            db = new QuanLyTiemNetDataContext();
            bool xoa = false;
            try
            {
                MAYTINH tim=db.MAYTINHs.Single(mt=>mt.MaMay.Equals(MaMay));
                if(tim!=null)
                {
                    db.MAYTINHs.DeleteOnSubmit(tim);
                    db.SubmitChanges();
                    xoa = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return xoa;
        }
        //đánh dấu xóa
        public static bool DanhDauXoa(ref string err, string MaMay,bool value)
        {
            db = new QuanLyTiemNetDataContext();
            bool danhdauxoa = false;
            try
            {
                MAYTINH tim=db.MAYTINHs.Single(mt=>mt.MaMay.Equals(MaMay));
                if(tim!=null)
                {
                    tim.DaXoa = value;
                    db.SubmitChanges();
                    danhdauxoa = true;
                }
            }

            catch(Exception e)
            {
                err = e.Message;
            }
            return danhdauxoa;
        }
        //ConverToDataTable
        public static DataTable ConverToDataTable(List<MAYTINH> list)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MaMay");
            dt.Columns.Add("MaPhong");
            dt.Columns.Add("DaCoNguoiThue");
            //dt.Columns.Add("DaXoa");
            foreach(var i in list)
            {
                dt.Rows.Add(i.MaMay, i.MaPhong, i.DaCoNguoiThue);
            }
            return dt;
        }

        //lấy thông tin
        public static DataTable LayThongTinMT()
        {
            db = new QuanLyTiemNetDataContext();
            var ttmt = from mt in db.MAYTINHs where mt.DaXoa==false select mt;
            return ConverToDataTable(ttmt.ToList());
        }
        //lấy lịch sử xóa máy tính
        public static DataTable LayLichSuMT()
        {
            db = new QuanLyTiemNetDataContext();
            var ttmt = from mt in db.MAYTINHs where mt.DaXoa == true select mt;
            return ConverToDataTable(ttmt.ToList());
        }
        //Lấy giá thuê máy
        public static float LayGiaThueMay(string MaMay)
        {
            PHONG phong = new PHONG();
            try
            {
                MAYTINH mt = db.MAYTINHs.Single(m => m.MaMay.Equals(MaMay));
                phong = db.PHONGs.Single(p => p.MaPhong.Equals(mt.MaPhong));
            }
            catch(Exception)
            {

            }
            return float.Parse(phong.GiaThueMay.ToString());
        }
       
    }
}
